package com.credithc.signature.manager;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

/**
 * @desc: <br/>
 * @author: Jingbo Hu<br/>
 * @time：2021年07⽉02⽇<br/>
 * @version：v1.0.0<br>
 */
public class UpgradeImage {

    public static void main(String[] args) throws Exception {
        String src = "D:\\吉林.jpg";
        String target = "D:\\吉林2.png";
        readImage(src, target, "png");
    }

    /**
     * 缩小图片
     *
     * @param src
     * @param target
     * @param imgFormate
     * @return
     */
    public static boolean readImage(String src, String target, String imgFormate) {
        //设定要生成的新图片的宽高和图片格式
        BufferedImage newbi = new BufferedImage(153, 153, BufferedImage.TYPE_INT_BGR);
        boolean val = false;
        try {
            //从硬盘读取一张图片
            BufferedImage oldbi = ImageIO.read(new File(src));
            Graphics2D gd = newbi.createGraphics();
            //设置透明 start
            newbi = gd.getDeviceConfiguration().createCompatibleImage(153, 153, Transparency.TRANSLUCENT);
            gd = newbi.createGraphics();
            gd.drawImage(oldbi, 0, 0, 153, 153, null);
            //通过ImageIO写到硬盘上去
            val = ImageIO.write(newbi, imgFormate, new File(target));
            return val;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return val;
    }

}
